/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.view;

import client.controler.ClickHandler;
import client.controler.LaunchController;
import client.controler.PackageReceiver;
import client.exceptions.NotInitializedException;
import client.model.ShipCounter;

import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.event.MouseInputListener;

import sharedObjects.Ships;

/**
 *
 * @author Niclas
 */
public class MainForm extends JFrame {


    
    private LaunchController lc;
    private final MainForm fform;
    private JToggleButton tbtnSchlachtschiff5;
    private JToggleButton tbtnKreuzer4;
    private JToggleButton tbtnZerstoerer3;
    private JToggleButton tbtnUboot2;
    private JLabel lbSchlachtschiff5Count;
    private JLabel lbKreuzer4Count;
    private JLabel lbZerstoerer3Count;
    private JLabel lbUboot2Count;
    public MainForm() throws HeadlessException {
    	fform = this;
    	
    	try
    	{
    	     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    	} catch(Exception e)
    	{}
    	
    	
    	lc = LaunchController.getInstance();
    	lc.gamemode.setMode(GameMode.POSITIONING_MODE);
    	RepaintNotifier.makeInstance(this);
        
        
        //FrameSettings
        JFrame frame = setupFrame();
        
        //Panel settings
        JPanel panel = setupPanel(frame);
        
        //Finish Positioning Button
        setupBtnFinishPositioning(panel);
        
        
        //Schlachtschiff Togglebutton
        tbtnSchlachtschiff5 = setupTbtnSchlachtschiff5(panel);
        
        //Kreuzer Togglebutton
        tbtnKreuzer4 = setupTbtnKreuzer4(panel);
        
        //Zerst�rer Togglebutton
        tbtnZerstoerer3 = setupTbtnZerstoerer3(panel);
        
        //Uboot Togglebotton
        tbtnUboot2 = setupTbtnUboot2(panel);
        
        //Schlachtschiff count label
        lbSchlachtschiff5Count = setupLbSchlachtschiff5Count(panel);
        
        //Kreuzer count label
        lbKreuzer4Count = setupLbKreuzer4Count(panel);
        
        //Zerst�rer count label
        lbZerstoerer3Count = setupLbZerstoerer3Count(panel);
        
        //Uboot count label
        lbUboot2Count = setupLbUboot2Count(panel);
        
        this.repaint();
    }
    
    private JFrame setupFrame(){
    	this.setVisible(true);
    	this.setTitle("Battleship Client");
        this.setSize(855, 600);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return this;
    }
    
    private JPanel setupPanel(JFrame frame){
    	
        JPanel panel = new JPanel() {
    	    @Override
                protected void paintComponent(Graphics g) {
    	    	
                    super.paintComponent(g);
                    Drawer drawer = new Drawer();
                    drawer.drawField(g);
                    drawer.drawHits(g);
                    drawer.drawShip(g);
                    
                }


            };

            panel.addMouseListener(new MouseInputListener() {

                @Override
                public void mouseClicked(MouseEvent e) {
                	
                    ClickHandler ch = ClickHandler.getInstance(fform);
                    if (lc.gamemode.getMode() == GameMode.POSITIONING_MODE){
                    ch.shipSegmentPlaced(e);
                    updateShipCountLabels();
                    }
                    if (lc.gamemode.getMode() == GameMode.BATTLE_MODE){
                        ch.shoot(e);
                        updateShipCountLabels();
                    }
                }

                @Override
                public void mousePressed(MouseEvent e) {
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                }

                @Override
                public void mouseExited(MouseEvent e) {
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                }

                @Override
                public void mouseMoved(MouseEvent e) {
                	
                }
            });
            panel.setSize(this.getSize());
            panel.setLocale(this.getLocale());
            panel.setLayout(null);
            panel.setBackground(new java.awt.Color(255, 255, 255));
            panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            panel.setVisible(true);
            panel.repaint();
            this.add(panel);
            return panel;
    }
    
    
	private void updateShipCountLabels() {
		ShipCounter sc = ShipCounter.getInstance();
		lbKreuzer4Count.setText(String.valueOf(sc.getKreuzer())+ "x");
		lbSchlachtschiff5Count.setText(String.valueOf(sc.getSchlachtschiff())+ "x");
		lbUboot2Count.setText(String.valueOf(sc.getUboot())+ "x");
		lbZerstoerer3Count.setText(String.valueOf(sc.getZerstoerer())+ "x");
		
		
	}
    
    private JButton setupBtnFinishPositioning(JPanel panel){
        JButton btnFinishPositioningMode = new JButton();
        btnFinishPositioningMode.setLocation(Drawer.MARGIN_LEFT, Drawer.MARGIN_TOP + (Drawer.Field_COUNT_VETICAL * Drawer.FIELD_HEIGHT)+ 2);
        btnFinishPositioningMode.setSize(401, 20);
        btnFinishPositioningMode.setText("Positionierung Beenden");
        btnFinishPositioningMode.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				lc.packageController.initialize();
				try {
					lc.packageController.sendField();
                                        lc.gamemode.setMode(GameMode.BATTLE_MODE);
				} catch (NotInitializedException e) {
					e.printStackTrace();
				}
				//lc.packageReceiver = new PackageReceiver(lc.package);
			}
		});
        panel.add(btnFinishPositioningMode);
        return btnFinishPositioningMode;
    }


    
    private JToggleButton setupTbtnSchlachtschiff5(JPanel panel){
    	JToggleButton tbtnSchlachtschiff5 = new JToggleButton(){
    		@Override
    		public void paint(Graphics g) {
    			super.paint(g);
    			for (int i = 0; i < 5;i++){
    			g.fillOval((i * 20) + 2, 2, 15, 15);
    			}
    		}
    	};
    	tbtnSchlachtschiff5.setLocation(Drawer.MARGIN_LEFT+ 30, Drawer.MARGIN_TOP + (Drawer.Field_COUNT_VETICAL * Drawer.FIELD_HEIGHT)+ 4 +20);
    	tbtnSchlachtschiff5.setSize(100,20);
    	tbtnSchlachtschiff5.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (((JToggleButton)arg0.getSource()).isSelected()){
				ClickHandler ch = ClickHandler.getInstance(fform);
				ch.shipSchlachtschiff5Selected();
				tbtnKreuzer4.setSelected(false);
				tbtnUboot2.setSelected(false);
				tbtnZerstoerer3.setSelected(false);
				}
				
			}
		});
    	panel.add(tbtnSchlachtschiff5);
    	return tbtnSchlachtschiff5;
    }
    
    private JLabel setupLbSchlachtschiff5Count(JPanel p){
    	ShipCounter sc = ShipCounter.getInstance();
    	JLabel lbSchlachtschiff5Count = new JLabel();
    	lbSchlachtschiff5Count.setLocation(Drawer.MARGIN_LEFT, Drawer.MARGIN_TOP + (Drawer.Field_COUNT_VETICAL * Drawer.FIELD_HEIGHT)+ 4 +20);
    	lbSchlachtschiff5Count.setSize(100, 20);
    	lbSchlachtschiff5Count.setText(sc.getSchlachtschiff() + "x");
    	p.add(lbSchlachtschiff5Count);
    	return lbSchlachtschiff5Count;
    }
    
    private JToggleButton setupTbtnKreuzer4(JPanel panel){
    	JToggleButton tbtnKreuzer4 = new JToggleButton(){
    		@Override
    		public void paint(Graphics g) {
    			super.paint(g);
    			for (int i = 0; i < 4;i++){
    				g.fillOval((i * 20) + 2, 2, 15, 15);
    			}
    		}
    	};
    	tbtnKreuzer4.setLocation(Drawer.MARGIN_LEFT+ 30, Drawer.MARGIN_TOP + (Drawer.Field_COUNT_VETICAL * Drawer.FIELD_HEIGHT)+ 4 + 2 +40);
    	tbtnKreuzer4.setSize(100,20);
    	tbtnKreuzer4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (((JToggleButton)arg0.getSource()).isSelected()){
				ClickHandler ch = ClickHandler.getInstance(fform);
				ch.shipKreuzer4Selected();
				tbtnSchlachtschiff5.setSelected(false);
				tbtnUboot2.setSelected(false);
				tbtnZerstoerer3.setSelected(false);
				}
				
			}
		});
    	panel.add(tbtnKreuzer4);
    	return tbtnKreuzer4;
    }
    
    private JLabel setupLbKreuzer4Count(JPanel p){
    	ShipCounter sc = ShipCounter.getInstance();
    	JLabel lbKreuzer4Count = new JLabel();
    	lbKreuzer4Count.setLocation(Drawer.MARGIN_LEFT, Drawer.MARGIN_TOP + (Drawer.Field_COUNT_VETICAL * Drawer.FIELD_HEIGHT)+ 4 + 2 +40);
    	lbKreuzer4Count.setSize(100, 20);
    	lbKreuzer4Count.setText(sc.getKreuzer() + "x");
    	p.add(lbKreuzer4Count);
    	return lbKreuzer4Count;
    }
    
    private JToggleButton setupTbtnZerstoerer3(JPanel panel){
    	JToggleButton tbtnZerstoerer3 = new JToggleButton(){
    		@Override
    		public void paint(Graphics g) {
    			super.paint(g);
    			for (int i = 0; i < 3;i++){
    				g.fillOval((i * 20) + 2, 2, 15, 15);
    			}
    		}
    	};
    	tbtnZerstoerer3.setLocation(Drawer.MARGIN_LEFT+ 30, Drawer.MARGIN_TOP + (Drawer.Field_COUNT_VETICAL * Drawer.FIELD_HEIGHT)+ 4 + 4 +60);
    	tbtnZerstoerer3.setSize(100,20);
    	tbtnZerstoerer3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (((JToggleButton)arg0.getSource()).isSelected()){
				ClickHandler ch = ClickHandler.getInstance(fform);
				ch.shipZerstoererSelected();
				tbtnKreuzer4.setSelected(false);
				tbtnUboot2.setSelected(false);
				tbtnSchlachtschiff5.setSelected(false);
				}
				
			}
		});
    	panel.add(tbtnZerstoerer3);
    	return tbtnZerstoerer3;
    }
    
    private JLabel setupLbZerstoerer3Count(JPanel p){
    	ShipCounter sc = ShipCounter.getInstance();
    	JLabel lbZerstoerer3Count = new JLabel();
    	lbZerstoerer3Count.setLocation(Drawer.MARGIN_LEFT, Drawer.MARGIN_TOP + (Drawer.Field_COUNT_VETICAL * Drawer.FIELD_HEIGHT)+ 4 + 4 +60);
    	lbZerstoerer3Count.setSize(100, 20);
    	lbZerstoerer3Count.setText(sc.getZerstoerer() + "x");
    	p.add(lbZerstoerer3Count);
    	return lbZerstoerer3Count;
    }
    
    private JToggleButton setupTbtnUboot2(JPanel panel){
    	JToggleButton tbtnUboot2 = new JToggleButton(){
    		@Override
    		public void paint(Graphics g) {
    			super.paint(g);
    			for (int i = 0; i < 2;i++){
    				g.fillOval((i * 20) + 2, 2, 15, 15);
    			}
    		}
    	};
    	tbtnUboot2.setLocation(Drawer.MARGIN_LEFT+ 30, Drawer.MARGIN_TOP + (Drawer.Field_COUNT_VETICAL * Drawer.FIELD_HEIGHT)+ 4 + 6 +80);
    	tbtnUboot2.setSize(100,20);
    	tbtnUboot2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (((JToggleButton)arg0.getSource()).isSelected()){
				ClickHandler ch = ClickHandler.getInstance(fform);
				ch.shipUbootSelected();
				tbtnKreuzer4.setSelected(false);
				tbtnSchlachtschiff5.setSelected(false);
				tbtnZerstoerer3.setSelected(false);
				}
				
			}
		});
    	panel.add(tbtnUboot2);
    	return tbtnUboot2;
    }
    
    private JLabel setupLbUboot2Count(JPanel p){
    	ShipCounter sc = ShipCounter.getInstance();
    	JLabel lbUboot2Count = new JLabel();
    	lbUboot2Count.setLocation(Drawer.MARGIN_LEFT, Drawer.MARGIN_TOP + (Drawer.Field_COUNT_VETICAL * Drawer.FIELD_HEIGHT)+ 4 + 6 +80);
    	lbUboot2Count.setSize(100, 20);
    	lbUboot2Count.setText(sc.getUboot() + "x");
    	p.add(lbUboot2Count);
    	return lbUboot2Count;
    }
    
    
    public void unselectAllToggleButtons(){
		tbtnKreuzer4.setSelected(false);
		tbtnSchlachtschiff5.setSelected(false);
		tbtnZerstoerer3.setSelected(false);
		tbtnUboot2.setSelected(false);
    }
    
    public void disableOtherToggleButtons(int i){
    	if (i == Ships.TYPE_KREUZER){
		tbtnKreuzer4.setEnabled(true);
		tbtnSchlachtschiff5.setEnabled(false);
		tbtnZerstoerer3.setEnabled(false);
		tbtnUboot2.setEnabled(false);;
    	}
    	if (i == Ships.TYPE_SCHLACHTSCHIFF){
		tbtnKreuzer4.setEnabled(false);
		tbtnSchlachtschiff5.setEnabled(true);
		tbtnZerstoerer3.setEnabled(false);
		tbtnUboot2.setEnabled(false);;
    	}
    	if (i == Ships.TYPE_UBOOTE){
		tbtnKreuzer4.setEnabled(false);
		tbtnSchlachtschiff5.setEnabled(false);
		tbtnZerstoerer3.setEnabled(false);
		tbtnUboot2.setEnabled(true);;
    	}
    	if (i == Ships.TYPE_ZERSTOERER){
		tbtnKreuzer4.setEnabled(false);
		tbtnSchlachtschiff5.setEnabled(false);
		tbtnZerstoerer3.setEnabled(true);
		tbtnUboot2.setEnabled(false);;
    	}
    }
    public void enableAllToggleButtons(){
		tbtnKreuzer4.setEnabled(true);
		tbtnSchlachtschiff5.setEnabled(true);
		tbtnZerstoerer3.setEnabled(true);
		tbtnUboot2.setEnabled(true);
    }
    
}
